import re
from bs4 import BeautifulSoup
import pandas as pd
import Data_Cofig as dc

Picture_suffix=['xbm','tif','pjp','svgz','jpg','jpeg','ico','tiff','gif','svg','jfif','webp','png','bmp','pjpeg','avif']

def find_Alllabel(text,label:str):
    soup = BeautifulSoup(text, "lxml", from_encoding="utf-8")
    result=soup.find_all(label)
    return result

def table_data(table):
    data=[]
    columns=[]
    thead=find_Alllabel(str(table),"thead")
    thead_trs=find_Alllabel(str(thead[0]),"tr")
    thead_ths=find_Alllabel(str(thead_trs),"th")
    for th in thead_ths:
        columns.append(th.get_text())
    tbody=find_Alllabel(str(table),"tbody")
    tbody_trs=find_Alllabel(str(tbody[0]),"tr")
    for tr in tbody_trs:
        tbody_tds=find_Alllabel(str(tr),"td")
        ths_data = []
        for td in tbody_tds:
            ths_data.append(td.get_text())
        data.append(ths_data)

    table=pd.DataFrame(data=data,columns=columns)
    return table
def tableParse(html,name='',cLass='',id=''):
    result_tables = []
    # thead tr th 1
    # tbody tr td n
    tables = find_Alllabel(html,"table")
    for table in tables:
        result_tables.append(table_data(table))
    return result_tables

def emailParse(text:str):
    # 邮箱解析
    pattern = re.compile(r"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)")
    result = pattern.findall(text)
    return result

def Chinese_card(text:str):
    # 国内身份证
    pattern = re.compile(r"[1-9]\d{5}(?:18|19|(?:[23]\d))\d{2}(?:(?:0[1-9])|(?:10|11|12))(?:(?:[0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]")
    result = pattern.findall(text)
    return result

def Chinese_PhoneNumber(text:str):
    # 国内手机号码
    pattern = re.compile(r"1[356789]\d{9}")
    result = pattern.findall(text)
    return result

def Chinese_landline_telephone(text:str):
    pattern = re.compile(r"\d{3}-\d{8}|\d{4}-\d{7}")
    result = pattern.findall(text)
    return result

def Domain_name(text:str):
    pattern = re.compile(r"(?:(?:http:\/\/)|(?:https:\/\/))?(?:[\w](?:[\w\-]{0,61}[\w])?\.)+[a-zA-Z]{2,6}(?:\/)")
    result = pattern.findall(text)
    return result

def IP_Parse(text:str):
    pattern = re.compile(r"((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))")
    result = pattern.findall(text)
    return result

def Date_Parse(text:str):
    pattern = re.compile(r"\d{4}(?:-|\/|.)\d{1,2}(?:-|\/|.)\d{1,2}")
    result = pattern.findall(text)
    return result

def Chinese_Zip_code(text:str):
    pattern = re.compile(r"[1-9]\d{5}(?!\d)")
    result = pattern.findall(text)
    return result
def StrongPassword(text:str):
    pattern = re.compile(r"[a-zA-Z](?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}")
    result = pattern.findall(text)
    return result

def User_Agent(text:str):
    user_agents=[]
    pattern = re.compile(r"((Mozilla|Opera)/\S+(\s\(.+?\)){0,1}(\s(?!Mozilla|Opera)\w+/\S+\s\(.+?\)){0,1}(\s(?!Mozilla|Opera)\w+/\S+|\s(?:Mobile|SE 2.X|MetaSr 1.0))*(\s\(.+?\)){0,1})")
    result = pattern.findall(text)
    for i in result:
        user_agents.append(i[0])
    return user_agents

def SickPassword(text:str):
    pattern = re.compile(r"[a-zA-Z]\w{5,17}")
    result = pattern.findall(text)
    return result

def Chinese_Character(text:str):
    pattern = re.compile(r"[\u4e00-\u9fa5]")
    result = pattern.findall(text)
    return result

def herf_Name_match(text:str,Image_suffix=[]):
    match_suffix=''
    for i in range(len(Image_suffix)):
        if i==0:
            match_suffix+=str(Image_suffix[i])
        else:
            match_suffix+=('|'+str(Image_suffix[i]))
    pattern = re.compile(r"(\w+)\.("+match_suffix+")")
    result = pattern.findall(text)
    return result

class Number_match:
    text:str=''
    def __int__(self,text):
        self.text=text
    def Z(text: str):
        pattern = re.compile(r"^-?\d+$")
        return pattern.findall(text)
    def Floating_point(text: str):
        pattern = re.compile(r"^(-?\d+)(\.\d+)?$")
        return pattern.findall(text)
    def x(text: str):
        pattern = re.compile(r"")
        return pattern.findall(text)
def ImageSourceParse(html):
    # 会将一个网页的img图片全部爬取下来放入一个类中，添加到列表中
    # 调用imag_Data类的方法，其中有下载功能，以及图片数据填充功能，还有通过链接补全图片名字的功能
    img_Datas=[]
    imgs=find_Alllabel(html,"img")
    for img in imgs:
        src=img.get("src")
        alt=img.get("alt")
        match_name=herf_Name_match(src, Picture_suffix)
        ImgName=match_name[0][0]+'.'+match_name[0][1]
        img_Data=dc.ImgData(ImgUrl=src,ImgName=ImgName,ImgIntroduce=alt)
        img_Datas.append(img_Data)
    return img_Datas







